package com.sgolub.commanndsonline.web;
   
import java.util.Map;

import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sgolub.commanndsonline.reports.DownloadService;
import com.sgolub.commanndsonline.reports.StatusResponse;
import com.sgolub.commanndsonline.reports.TokenService;
 
@Controller
@RequestMapping("/reports")
public class ReportsController { 
	
	@Autowired
	private TokenService tokenService;
	
	@Autowired
	private DownloadService downloadService;
	
    @RequestMapping(value="{menuName}", method = RequestMethod.GET)
	public String generate(Map<String, Object> map) {
    	//Report generating stub;
		return "";
	}    
     
    @RequestMapping("/reports") 
    public String reports(Map<String, Object> map)  {
    System.out.println("AppController - reports");
    
    return "reports";
    }    
 
    
	@RequestMapping(value="/download/token")
	public @ResponseBody StatusResponse getDownloadToken() {
		return new StatusResponse(true, tokenService.generate());
	}
	
	@RequestMapping(value="/download/progress")
	public @ResponseBody StatusResponse checkDownloadProgress(@RequestParam String token) {
		return new StatusResponse(true, tokenService.check(token));
	}
	
	@RequestMapping(value="/download/report")
	public void download(@RequestParam String type,
			@RequestParam String token, @RequestParam Integer orderId,
			HttpServletResponse response) {
		downloadService.download(orderId, type, token, response);
	}
}